home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / cvs-1_3.lha / cvs-1.3 / src / parseinfo.c < prev    next >
C/C++ Source or Header  |  1992-04-10  |  4KB  |  148 lines

  1. /*
  2.  * Copyright (c) 1992, Brian Berliner and Jeff Polk
  3.  * Copyright (c) 1989-1992, Brian Berliner
  4.  * 
  5.  * You may distribute under the terms of the GNU General Public License as
  6.  * specified in the README file that comes with the CVS 1.3 kit.
  7.  */
  8.  
  9. #include "cvs.h"
  10.  
  11. #ifndef lint
  12. static char rcsid[] = "@(#)parseinfo.c 1.16 92/04/10";
  13. #endif
  14.  
  15. /*
  16.  * Parse the INFOFILE file for the specified REPOSITORY.  Invoke CALLPROC for
  17.  * each line in the file that matches the REPOSITORY.  
  18.  * Return 0 for success, -1 if there was not an INFOFILE, and >0 for failure.
  19.  */
  20. int
  21. Parse_Info (infofile, repository, callproc, all)
  22.     char *infofile;
  23.     char *repository;
  24.     int (*callproc) ();
  25.     int all;
  26. {
  27.     int err = 0;
  28.     FILE *fp_info;
  29.     char infopath[PATH_MAX];
  30.     char line[MAXLINELEN];
  31.     char *default_value = NULL;
  32.     int callback_done, line_number;
  33.     char *cp, *exp, *value, *srepos;
  34.     CONST char *regex_err;
  35.  
  36.     if (CVSroot == NULL)
  37.     {
  38.     /* XXX - should be error maybe? */
  39.     error (0, 0, "CVSROOT variable not set");
  40.     return (1);
  41.     }
  42.  
  43.     /* find the info file and open it */
  44.     (void) sprintf (infopath, "%s/%s/%s", CVSroot,
  45.             CVSROOTADM, infofile);
  46.     if ((fp_info = fopen (infopath, "r")) == NULL)
  47.     return (0);            /* no file -> nothing special done */
  48.  
  49.     /* strip off the CVSROOT if repository was absolute */
  50.     srepos = Short_Repository (repository);
  51.  
  52.     /* search the info file for lines that match */
  53.     callback_done = line_number = 0;
  54.     while (fgets (line, sizeof (line), fp_info) != NULL)
  55.     {
  56.     line_number++;
  57.  
  58.     /* skip lines starting with # */
  59.     if (line[0] == '#')
  60.         continue;
  61.  
  62.     /* skip whitespace at beginning of line */
  63.     for (cp = line; *cp && isspace (*cp); cp++)
  64.         ;
  65.  
  66.     /* if *cp is null, the whole line was blank */
  67.     if (*cp == '\0')
  68.         continue;
  69.  
  70.     /* the regular expression is everything up to the first space */
  71.     for (exp = cp; *cp && !isspace (*cp); cp++)
  72.         ;
  73.     if (*cp != '\0')
  74.         *cp++ = '\0';
  75.  
  76.     /* skip whitespace up to the start of the matching value */
  77.     while (*cp && isspace (*cp))
  78.         cp++;
  79.  
  80.     /* no value to match with the regular expression is an error */
  81.     if (*cp == '\0')
  82.     {
  83.         error (0, 0, "syntax error at line %d file %s; ignored",
  84.            line_number, infofile);
  85.         continue;
  86.     }
  87.     value = cp;
  88.  
  89.     /* strip the newline off the end of the value */
  90.     if ((cp = rindex (value, '\n')) != NULL)
  91.         *cp = '\0';
  92.  
  93.     /*
  94.      * At this point, exp points to the regular expression, and value
  95.      * points to the value to call the callback routine with.  Evaluate
  96.      * the regular expression against srepos and callback with the value
  97.      * if it matches.
  98.      */
  99.  
  100.     /* save the default value so we have it later if we need it */
  101.     if (strcmp (exp, "DEFAULT") == 0)
  102.     {
  103.         default_value = xstrdup (value);
  104.         continue;
  105.     }
  106.  
  107.     /*
  108.      * For a regular expression of "ALL", do the callback always We may
  109.      * execute lots of ALL callbacks in addition to one regular matching
  110.      * callback or default
  111.      */
  112.     if (strcmp (exp, "ALL") == 0)
  113.     {
  114.         if (all)
  115.         err += callproc (repository, value);
  116.         else
  117.         error(0, 0, "Keyword `ALL' is ignored at line %d in %s file",
  118.               line_number, infofile);
  119.         continue;
  120.     }
  121.  
  122.     /* see if the repository matched this regular expression */
  123.     if ((regex_err = re_comp (exp)) != NULL)
  124.     {
  125.         error (0, 0, "bad regular expression at line %d file %s: %s",
  126.            line_number, infofile, regex_err);
  127.         continue;
  128.     }
  129.     if (re_exec (srepos) == 0)
  130.         continue;            /* no match */
  131.  
  132.     /* it did, so do the callback and note that we did one */
  133.     err += callproc (repository, value);
  134.     callback_done = 1;
  135.     }
  136.     (void) fclose (fp_info);
  137.  
  138.     /* if we fell through and didn't callback at all, do the default */
  139.     if (callback_done == 0 && default_value != NULL)
  140.     err += callproc (repository, default_value);
  141.  
  142.     /* free up space if necessary */
  143.     if (default_value != NULL)
  144.     free (default_value);
  145.  
  146.     return (err);
  147. }
  148.